package com.yylending.las.authority.password;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import butterknife.*;
import com.yyfax.common.CLog;
import com.yyfax.common.tools.RegexUtil;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.R;
import com.yylending.las.StatIDSet;
import com.yylending.las.YJKD;
import com.yylending.las.base.BaseActivity;
import com.yylending.las.main.MainActivity;

/**
 * Created by HenryZhang on 2016/11/18.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class ModifyPwdActivity extends BaseActivity implements ModifyPwdPresenter.View {
	private final static String TAG = "ModifyPwdActivity";

	private final static int LOADER_ID = 204;

	@BindView(R.id.modify_pwd_old_pwd)
	EditText mOldPwd;
	@BindView(R.id.modify_pwd_old_delete)
	ImageView mOldDelete;
	@BindView(R.id.modify_pwd_new_pwd)
	EditText mNewPwd;
	@BindView(R.id.modify_pwd_new_delete)
	ImageView mNewDelete;
	@BindView(R.id.modify_pwd_new_confirm)
	EditText mNewConfirm;
	@BindView(R.id.modify_pwd_new_confirm_delete)
	ImageView mNewConfirmDelete;
	@BindView(R.id.modify_pwd_submit)
	Button mSubmit;

	private ModifyPwdPresenter mPresenter;

	public static void start(Activity aty) {
		Intent i = new Intent(aty, ModifyPwdActivity.class);
		aty.startActivity(i);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle bundle) {
		switch (id) {
			case LOADER_ID:
				CLog.i(TAG, "ModifyPwdActivity onCreateLoader");
				return new PresenterLoader<>(this, new ModifyPwdPresenter.Generator());
			default:
				return null;
		}
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		switch (loader.getId()) {
			case LOADER_ID:
				CLog.i(TAG, "ModifyPwdActivity onLoadFinished");
				mPresenter = (ModifyPwdPresenter) data;
				mPresenter.attach(this);
				break;
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_pwd);
		ButterKnife.bind(this);
		setTitle(R.string.title_activity_modify_pwd);
		getSupportLoaderManager().initLoader(LOADER_ID, null, this);
	}

	@OnClick({R.id.modify_pwd_submit, R.id.modify_pwd_old_delete, R.id.modify_pwd_new_delete, R.id.modify_pwd_new_confirm_delete})
	public void onViewClicked(View view) {// TODO: 2017/5/24 判断新旧密码是否一致
		switch (view.getId()) {
			case R.id.modify_pwd_submit:
				YJKD.hideInputWindow(this);
				String oldPwd = mOldPwd.getText().toString().trim();
				String newPwd = mNewPwd.getText().toString().trim();
				String newPwdCom = mNewConfirm.getText().toString().trim();
				int passwordMinLength = 6;

				if (TextUtils.isEmpty(oldPwd)) {
					showToast(getString(R.string.please_input_toast, getString(R.string.old_password)));
				} else if (TextUtils.isEmpty(newPwd)) {
					showToast(getString(R.string.please_input_toast, getString(R.string.new_password)));
				} else if (TextUtils.isEmpty(newPwdCom)) {
					showToast(getString(R.string.please_input_toast, getString(R.string.confirm_password)));
					//新密码和确认密码长度不够,或者纯数字或纯字母弹提示
				} else if (newPwd.length() < passwordMinLength || newPwdCom.length() < passwordMinLength || !RegexUtil.isRightPwd(newPwd) || !RegexUtil.isRightPwd(newPwdCom)) {
					showToast(R.string.please_set_right_password);
				} else if (!newPwd.equals(newPwdCom)) {
					showToast(R.string.confirm_pwd_different);
				} else if (newPwd.equals(oldPwd)) {
					showToast(R.string.new_password_same_with_old);
				}else {
					CLog.i(TAG, "修改密码");
					mPresenter.modify(oldPwd, newPwd);
				}
				break;
			case R.id.modify_pwd_old_delete:
				mOldPwd.getText().clear();
				break;
			case R.id.modify_pwd_new_delete:
				mNewPwd.getText().clear();
				break;
			case R.id.modify_pwd_new_confirm_delete:
				mNewConfirm.getText().clear();
				break;
		}
	}

	@Override
	public void onResetPwdSuccess() {
		showToast(R.string.pwd_reset_success);
		CLog.i(TAG, "需要重新登录");
		YJKD.logout();
		MainActivity.start(this, true);
	}

	@Override
	public void onResetPwdFailure(String msg) {
		showToast(msg);
	}

	@Override
	public void onResetPwdRetCode(String retCode) {
		StatService.onEventWithSingleAttribute(this, StatIDSet.EVENT_ID_CHANGE_PASSWORD,retCode);
	}

	@OnCheckedChanged(R.id.modify_pwd_old_visibility)
	public void onOldPwdChecked(CompoundButton btn, boolean checked) {
		if(checked){
			mOldPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
		}else{
			mOldPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
		}
		mOldPwd.setSelection(mOldPwd.getText().length());//设置光标到最末尾
	}

	@OnCheckedChanged(R.id.modify_pwd_new_visibility)
	public void onNewPwdChecked(CompoundButton btn, boolean checked) {
		if(checked){
			mNewPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
		}else{
			mNewPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
		}
		mNewPwd.setSelection(mNewPwd.getText().length());//设置光标到最末尾
	}

	@OnCheckedChanged(R.id.modify_pwd_new_confirm_visibility)
	public void onConfirmPwdChecked(CompoundButton btn, boolean checked) {
		if(checked){
			mNewConfirm.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
		}else{
			mNewConfirm.setTransformationMethod(PasswordTransformationMethod.getInstance());
		}
		mNewConfirm.setSelection(mNewConfirm.getText().length());//设置光标到最末尾
	}

	@OnTextChanged(value = R.id.modify_pwd_old_pwd, callback = OnTextChanged.Callback.TEXT_CHANGED)
	public void onOldPwdChanged(CharSequence s, int start, int before, int count) {
		if (mOldPwd.getText().toString().trim().length() != 0) {
			mOldDelete.setVisibility(View.VISIBLE);
		} else {
			mOldDelete.setVisibility(View.GONE);
		}
	}

	@OnTextChanged(value = R.id.modify_pwd_new_pwd, callback = OnTextChanged.Callback.TEXT_CHANGED)
	public void onNewPwdChanged(CharSequence s, int start, int before, int count) {
		if (mNewPwd.getText().toString().trim().length() != 0) {
			mNewDelete.setVisibility(View.VISIBLE);
		} else {
			mNewDelete.setVisibility(View.GONE);
		}
	}

	@OnTextChanged(value = R.id.modify_pwd_new_confirm, callback = OnTextChanged.Callback.TEXT_CHANGED)
	public void onConfirmPwdChanged(CharSequence s, int start, int before, int count) {
		if (mNewConfirm.getText().toString().trim().length() != 0) {
			mNewConfirmDelete.setVisibility(View.VISIBLE);
		} else {
			mNewConfirmDelete.setVisibility(View.GONE);
		}
	}

	@Override
	protected void onDestroy() {
		CLog.i(TAG, "ModifyPwdActivity destroyLoader LOADER_ID");
		getSupportLoaderManager().destroyLoader(LOADER_ID);
		super.onDestroy();
	}
}
